package com.source.syslog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.source.baseData.vo.SlaughterHouseRepairVo;
import com.source.labelAndBarcode.vo.SlaughterHouseVo;
import com.source.syslog.entity.Operation;
import com.source.syslog.service.OperationService;
import com.source.utils.CommonResult;
import com.source.utils.MyPage;
import com.source.utils.ResultEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author IceBerg777
 * @since 2021-04-11
 */
@Api(tags = "日志接口")
@RestController
@RequestMapping("/syslog/operation")
@CrossOrigin
public class OperationController {

    @Autowired
    private OperationService operationService;

    //根据id删除
    @ApiOperation("根据id删除")
    @DeleteMapping(value = "/deleteById/{id}")
    public CommonResult<String> deleteById(@PathVariable String id) {
        try {
            if(operationService.removeById(id)){
                return new CommonResult<String>(ResultEnum.SUCCESS);
            }
            return new CommonResult<String>(ResultEnum.ERROR);
        }catch (Exception e){
            return new CommonResult<String>(ResultEnum.ERROR);
        }
    }

    //批量删除
    @ApiOperation("根据id批量删除")
    @DeleteMapping(value = "/deleteByIds/{ids}")
    public CommonResult<String> deleteByIds(@PathVariable List<String> ids) {
        try {
            if(operationService.removeByIds(ids)){
                return new CommonResult<String>(ResultEnum.SUCCESS);
            }
            return new CommonResult<String>(ResultEnum.ERROR);
        }catch (Exception e){
            return new CommonResult<String>(ResultEnum.ERROR);
        }
    }


    @ApiOperation("根据日志主键id查询日志")
    @RequestMapping(value = "/findById/{id}",method = RequestMethod.GET)
    public CommonResult<Operation> findById(@PathVariable String id) {
        return new CommonResult<Operation>(ResultEnum.SUCCESS, operationService.getById(id));
    }

    //查找全部
    @ApiOperation("查找所有，返回list集合")
    @GetMapping("/findAll")
    public CommonResult<List<Operation>> findAll() {
        return new CommonResult<List<Operation>>(ResultEnum.SUCCESS, operationService.list());
    }

    @ApiOperation("分页查找所有，返回list集合")
    @GetMapping
    public CommonResult<Page<Operation>> findAllPage(@RequestParam(value = "current", defaultValue = "1") Integer current,
                                                             @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Page<Operation> page = new Page<>(current, size);
        Page page1 = MyPage.list2Page(current, size, findAll().getData());
        return new CommonResult<Page<Operation>>(ResultEnum.SUCCESS,page1);
    }

    @ApiOperation(value = "根据关键词模糊查询")
    @GetMapping("/findByKeyWord/{keyWord}/{current}/{size}")
    public CommonResult<Page> findByKeyWord(@PathVariable String keyWord,
                                            @PathVariable Integer current, @PathVariable Integer size) {
        List<Operation> list = operationService.findByKeyWord(keyWord);
        Page page = MyPage.list2Page(current, size, list);
        return new CommonResult<Page>(ResultEnum.SUCCESS, page);
    }


    @ApiOperation("根据用户id，分页查找所有，返回list集合")
    @GetMapping("/operationByUserId/{id}")
    public CommonResult<Page<Operation>> findByPersonId(@PathVariable String id, @RequestParam(value = "current", defaultValue = "1") Integer current,
                                          @RequestParam(value = "size", defaultValue = "10") Integer size){
        Page<Operation> page = new Page<>(current, size);
        QueryWrapper<Operation> wrapper = new QueryWrapper<>();
        wrapper.eq("userId",id);
        List<Operation> list = operationService.list(wrapper);
        Page page1 = MyPage.list2Page(current, size, list);

        return new CommonResult<Page<Operation>>(ResultEnum.SUCCESS,page1);
    }
}

